<?php
/**
 * Created by PhpStorm.
 * User: envision
 * Date: 2018-09-28
 * Time: 9:12
 */

namespace App\Http\Controllers\Api\V1\Manager;


use App\Http\Controllers\Controller;
use App\Models\RechangRule;
use Illuminate\Http\Request;


class RechangRuleController extends Controller
{
    public function index(Request $request)
    {
        $sql = RechangRule::select();

        $data = $sql->orderBy('sort')->get()->toArray();
        return $this->apiResponse($data);

    }

   /* public function show($userId)
    {

    }*/

    public function store(Request $request)
    {

        $data = $request->only('rechangRuleId','money','sort');
        $rechangRuleId= $data['rechangRuleId'];
        unset($data['rechangRuleId']);
        if($rechangRuleId){
            $status = RechangRule::where('rechangRuleId',$rechangRuleId)->update($data);
            if(!$status){
                return $this->apiResponse('', config('errorCode.NOT_UPDATE'));
            }
        }else{
            $data['createTime'] = date('Y-m-d H:i:s');
            $status = RechangRule::create($data);
        }
        if(!$status){
            return $this->apiResponse('', config('errorCode.UPDATE_FAILED'));
        }
        return $this->apiResponse('');
    }

    public function destroy($rechangRuleId)
    {
        RechangRule::destroy($rechangRuleId);
        return $this->apiResponse('');
    }


}